from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import SingletonThreadPool, NullPool

Base = declarative_base()
engine = create_engine('sqlite:///ai-ele.db', connect_args={'check_same_thread': False},
                       poolclass=SingletonThreadPool, echo=False)
engine.execute('pragma foreign_keys=on')

#engine.execute('update Controller set name = ? where id = ?', (1, 'name'))

Session = sessionmaker(bind=engine, autoflush=True)

def make_new_engine_new_session():
    engine1 = create_engine('sqlite:///ai-ele.db', connect_args={'check_same_thread': True},
                            poolclass=SingletonThreadPool, echo=False)
    engine1.execute('pragma foreign_keys=on')
    session = sessionmaker(bind=engine1, autoflush=True)
    return session()
